/****************************************************************************
  @file    : tvbox_90.c
  @date    : 2021-1-25
  @brief   : 
*****************************************************************************/
/* Includes ----------------------------------------------------------------*/
#include "remote.h"
#include "string.h"
#include "tvbox.h"

/* Private typedef ---------------------------------------------------------*/
/* Private define ----------------------------------------------------------*/
//ʱ�䶨��
#define  BOX_LEADCODE_TIME_H		(uint16_t)( 0x8000   | (uint16_t)8970 )
#define  BOX_LEADCODE_TIME_L		(uint16_t)( (~0x8000)& (uint16_t)4440 )

#define  BOX_STOPCODE_TIME_H		(uint16_t)( 0x8000   | (uint16_t)600  )
#define  BOX_STOPCODE_TIME_L		(uint16_t)( (~0x8000)& (uint16_t)32767)
#define  BOX_REPEATCODE_TIME_H	(uint16_t)( 0x8000   | (uint16_t)8970  )
#define  BOX_REPEATCODE_TIME_L	(uint16_t)( (~0x8000)& (uint16_t)2200)

#define  BOX_ENDCODE_TIME_H			(uint16_t)( 0x8000   | (uint16_t)610  )
#define  BOX_ENDCODE_TIME_L	    (uint16_t)( (~0x8000)& (uint16_t)20000)
#define  BOX_BIT0_TIME_H			(uint16_t)( 0x8000   | (uint16_t)610  )
#define  BOX_BIT0_TIME_L			(uint16_t)( (~0x8000)& (uint16_t)540  )
#define  BOX_BIT1_TIME_H			(uint16_t)( 0x8000   | (uint16_t)610  )
#define  BOX_BIT1_TIME_L			(uint16_t)( (~0x8000)& (uint16_t)1640 )

//������HOF19K06������ʱ�䶨��
#define  HOF_BOX_LEADCODE_TIME_H		(uint16_t)( 0x8000   | (uint16_t)2630 )
#define  HOF_BOX_LEADCODE_TIME_L		(uint16_t)( (~0x8000)& (uint16_t)920  )
//#define  BOX_ENDCODE_TIME_H			(uint16_t)( 0x8000   | (uint16_t)560  )
#define  HOF_BOX_ENDCODE_TIME_L	    	(uint16_t)( (~0x8000)& (uint16_t)25000)

#define  HOF_BOX_T0_TIME_H				(uint16_t)( (~0x8000)& (uint16_t)889  )
#define  HOF_BOX_T0_TIME_L				(uint16_t)( 0x8000   | (uint16_t)889  )
#define  HOF_BOX_T1_TIME_H				(uint16_t)( 0x8000   | (uint16_t)889  )
#define  HOF_BOX_T1_TIME_L				(uint16_t)( (~0x8000)& (uint16_t)889  )

#define  HOF_BOX_BIT0_TIME_H			(uint16_t)( (~0x8000)& (uint16_t)444  )
#define  HOF_BOX_BIT0_TIME_L			(uint16_t)( 0x8000   | (uint16_t)444  )
#define  HOF_BOX_BIT1_TIME_H			(uint16_t)( 0x8000   | (uint16_t)444  )
#define  HOF_BOX_BIT1_TIME_L			(uint16_t)( (~0x8000)& (uint16_t)444  )

//��ֵ����
static const uint8_t BOX_Code[TVBOX_KEY_NUM] = {
 0x00 ,//0x01����չ-���أ�
 0x00 ,//0x02����չ-AV/TV��
 0x00 ,//0x03����չ-����+��
 0x00 ,//0x04����չ-����-��
 0x00 ,//0x05����չ-Ƶ��+��
 0x00 ,//0x06����չ-Ƶ��-��
 0x00 ,//0x07����չ-������
 0x00 ,//0x08����չ-������
 0x00 ,//0x09����չ-������
 0x00 ,//0x0A����չ-������
 0x18 ,//0x18�����أ�
 0x04 ,//0x04���˵���
 0x00 ,//0x00����Ϣ��
 0x40 ,//0x40�����أ�
 0x5B ,//0x5B��������
 0x00 ,//0x00��������
 0x14 ,//0x14������+��
 0x10 ,//0x10������-��
 0x50 ,//0x50��Ƶ��+��
 0x52 ,//0x52��Ƶ��-��
 0x46 ,//0x46���ϣ�
 0x16 ,//0x16���£�
 0x47 ,//0x47����
 0x15 ,//0x15���ң�
 0x1C ,//0x1C��ȷ����
 0x00 ,//0x00���ź�Դ��
 0x4E ,//0x4E����ҳ��
 0x00 ,//0x00��������
 0x00 ,//0x00��������
 0x1D ,//0x1D������0��
 0x0F ,//0x0F������1��
 0x11 ,//0x11������2��
 0x12 ,//0x12������3��
 0x4C ,//0x4C������4��
 0x58 ,//0x58������5��
 0x1B ,//0x1B������6��
 0x17 ,//0x17������7��
 0x4D ,//0x4D������8��
 0x0A ,//0x0A������9��
 0x00 ,//0x00�����뷨��
};

/* Private macro -----------------------------------------------------------*/
/* Private variables -------------------------------------------------------*/
static uint16_t BoxSendlen = 0;
static uint8_t BitMask = 0;

/* Private function prototypes ---------------------------------------------*/
static void PackageByte(uint8_t byte);
/* Private function prototypes ---------------------------------------------*/
static void HOF_PackageByte(uint8_t byte);

/* Private functions -------------------------------------------------------*/
static void PackageByte(uint8_t byte) {
	uint8_t i;

	for(i = 0; i<8; i++) {
		if(byte&(0x01<<(i))) {	//�ߵ�ƽ
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_L;
		} else {				//�͵�ƽ
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_L;
		}
	}
}

/* Private functions -------------------------------------------------------*/
static void HOF_PackageByte(uint8_t byte)
{
	uint8_t i;

	for(i = 0; i<8; i++) {
		if(byte&(0x80>>(i))) {	//�ߵ�ƽ
			IrSendBuf[BoxSendlen++] = HOF_BOX_BIT1_TIME_H;
			IrSendBuf[BoxSendlen++] = HOF_BOX_BIT1_TIME_L;
		} else {				//�͵�ƽ
			IrSendBuf[BoxSendlen++] = HOF_BOX_BIT0_TIME_H;
			IrSendBuf[BoxSendlen++] = HOF_BOX_BIT0_TIME_L;
		}
	}
}

/* Public functions --------------------------------------------------------*/
void TVBox90IrPackage(void *p) {
	uint8_t  i,p_byte = 0x00;

	uint8_t codeNum = *(uint8_t*)p;

	if(codeNum == 0 || codeNum >= TVBOX_KEY_NUM) return ;
	
	p_byte = BOX_Code[codeNum-1];

	if(p_byte == 0x00) return;
	
	//�ȴ��������
	//while(IrDev.State != IRREMOTE_IDLE);  
	memset(IrSendBuf, 0, sizeof(IrSendBuf)/sizeof(uint16_t));
	
	BoxSendlen = 0;

	if(codeNum==0x0B)	{	//������
		//Lead code
		IrSendBuf[BoxSendlen++] = HOF_BOX_LEADCODE_TIME_H;
		IrSendBuf[BoxSendlen++] = HOF_BOX_LEADCODE_TIME_L;
	
		//S
		IrSendBuf[BoxSendlen++] = HOF_BOX_BIT1_TIME_H;
		IrSendBuf[BoxSendlen++] = HOF_BOX_BIT1_TIME_L;
	
		//S
		IrSendBuf[BoxSendlen++] = HOF_BOX_BIT0_TIME_H;
		IrSendBuf[BoxSendlen++] = HOF_BOX_BIT0_TIME_L;
		IrSendBuf[BoxSendlen++] = HOF_BOX_BIT0_TIME_H;
		IrSendBuf[BoxSendlen++] = HOF_BOX_BIT0_TIME_L;
		IrSendBuf[BoxSendlen++] = HOF_BOX_BIT0_TIME_H;
		IrSendBuf[BoxSendlen++] = HOF_BOX_BIT0_TIME_L;
	
		// toggle
		++ BitMask;
	
		if(BitMask&0x01) {
			IrSendBuf[BoxSendlen++] = HOF_BOX_T1_TIME_H;
			IrSendBuf[BoxSendlen++] = HOF_BOX_T1_TIME_L;
		} else {
			IrSendBuf[BoxSendlen++] = HOF_BOX_T0_TIME_H;
			IrSendBuf[BoxSendlen++] = HOF_BOX_T0_TIME_L;
		}
	
		//���ÿ�����
		HOF_PackageByte(0x00);
		HOF_PackageByte(0x0C);
	
		//���ý�����
		IrSendBuf[BoxSendlen++] = HOF_BOX_ENDCODE_TIME_L;
		IrSendBuf[BoxSendlen++] = HOF_BOX_ENDCODE_TIME_L;
		IrSendBuf[BoxSendlen++] = HOF_BOX_ENDCODE_TIME_L;
		IrSendBuf[BoxSendlen++] = HOF_BOX_ENDCODE_TIME_L;
	}
	else {
		//����������
		IrSendBuf[BoxSendlen++] = BOX_LEADCODE_TIME_H;
		IrSendBuf[BoxSendlen++] = BOX_LEADCODE_TIME_L;
	
		//���ÿ�����
		PackageByte(0x00);
		PackageByte(0xff);
		PackageByte(p_byte);
		PackageByte(~p_byte);
	
		//����ֹͣ��
		IrSendBuf[BoxSendlen++] = BOX_STOPCODE_TIME_H;
		IrSendBuf[BoxSendlen++] = BOX_STOPCODE_TIME_L;
		
		//�����ظ���
		IrSendBuf[BoxSendlen++] = BOX_REPEATCODE_TIME_H;
		IrSendBuf[BoxSendlen++] = BOX_REPEATCODE_TIME_L;
		
		//���ý�����
		IrSendBuf[BoxSendlen++] = BOX_ENDCODE_TIME_H;
		IrSendBuf[BoxSendlen++] = BOX_ENDCODE_TIME_L;
	}
	
	//�������ύ
	IrDev.Ptr 		= IrSendBuf;
	IrDev.Len 		= BoxSendlen;
	IrDev.State 	= IRREMOTE_REQUIRE;
}
